package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

import cn.com.utt.common.exception.BusinessException;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.PageList;
import cn.com.utt.unms.pojo.WifiConf;
import cn.com.utt.unms.pojo.Wireless;
import cn.com.utt.unms.service.AuthPageService;
import cn.com.utt.unms.service.WifiCfgService;
import cn.com.utt.unms.vo.ShopList;
import cn.com.utt.unms.vo.WifiConfInfo;

@RestController
@RequestMapping("v1/portal")
public class PortalController {

	@Autowired
	private WifiCfgService wifiCfgService;

	@Autowired
	private AuthPageService authPageService;

	@RequestMapping(value = "/info", method = { RequestMethod.GET })
	public Map<String, Object> info(Long cfgId, Principal principal) {

		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();

		WifiConf wifiConf = wifiCfgService.queryWifiConfById(cfgId);
		List<PageList> plist = authPageService.queryAll(user.getId());
		result.put("wifiConf", wifiConf);
		result.put("page", plist);
		return result.toJsonObject();
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public Map<String, Object> list(@RequestBody TableInput input, Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<WifiConfInfo> output = wifiCfgService.queryByCondition(input, user.getId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("confs", output.getList());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/all", method = { RequestMethod.GET })
	public Map<String, Object> all(Principal principal) {

		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();

		List<WifiConf> confs = wifiCfgService.queryAll(user.getId());
		SimplePropertyPreFilter confFilter = new SimplePropertyPreFilter(WifiConf.class, "id", "name");
		result.put("confs", JSON.toJSONString(confs, confFilter));
		return result.toJsonObject();
	}

	@RequestMapping(value = "/page", method = { RequestMethod.GET })
	public Map<String, Object> page(Long cfgId, Principal principal) {

		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();

		List<PageList> plist = authPageService.queryAll(user.getId());
		result.put("page", plist);
		return result.toJsonObject();
	}

	@RequestMapping(value = "/save", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> save(@RequestBody WifiConf wifiConf, Principal principal) {

		System.out.println("------------wifiConf save");
		System.out.println(JSON.toJSONString(wifiConf));
		RestResult rest = new RestResult();

		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();

		try {
			wifiConf.setUserId(user.getId());
			wifiCfgService.saveWifiConf(wifiConf);
			rest.setErrorMsg("添加成功");
		} catch (Exception e) {
			rest.setErrorCode(1);
			rest.setErrorMsg("认证设置添加失败！" + e.getMessage());
		}
		return rest.toJsonObject();
	}

	@RequestMapping(value = "/update", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> update(@RequestBody WifiConf wifiConf) {

		System.out.println("------------wifiConf update");
		System.out.println(JSON.toJSONString(wifiConf));
		RestResult rest = new RestResult();
		try {
			wifiCfgService.updateWifiConf(wifiConf, wifiConf.getId());
		} catch (BusinessException e) {
			rest.setErrorCode(1);
			rest.setErrorMsg("认证设置失败！" + e.getMessage());
		}
		return rest.toJsonObject();
	}

	@RequestMapping(value = "/shops", method = { RequestMethod.POST })
	public Map<String, Object> shops(Long cfgId) {

		RestResult result = new RestResult();

		try {
			ShopList list = wifiCfgService.queryWXShopList(cfgId);
			result.put("shopList", list);
		} catch (BusinessException e) {
			result.setErrorCode(1);
			result.setErrorMsg(e.getMessage());
		}
		return result.toJsonObject();
	}

	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "cfgIds[]") List<Long> cfgIds, Principal principal) {
		RestResult result;
		System.out.println(cfgIds);
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = wifiCfgService.delete(cfgIds, user.getId());
		return result.toJsonObject();
	}

}